package com.neusoft.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.neusoft.feign.FoodFeignClient;
import com.neusoft.po.Business;
import com.neusoft.po.CommonResult;
import com.neusoft.service.BusinessService;

//@CrossOrigin("*")
@RestController
//添加访问路径
@RequestMapping("/BusinessController")
@RefreshScope

public class BusinessController {
	@Autowired
	private BusinessService businessService;

	/*
	 使用openFeign，不需要这两个
	 
	 @Autowired private RestTemplate restTemplate;
	 
	 @Autowired private DiscoveryClient discoveryClient;
	 */
	@Autowired
	private FoodFeignClient foodFeignClient;

	@GetMapping("/listBusinessByOrderTypeId/{orderTypeId}")
	public CommonResult<List> listBusinessByOrderTypeId(@PathVariable("orderTypeId") Integer orderTypeId) throws Exception {
		List<Business> list = businessService.listBusinessByOrderTypeId(orderTypeId);
		return new CommonResult(200, "success(10301)", list);
	}

	@GetMapping("/getBusinessById/{businessId}")
	public CommonResult<Business> getBusinessById(@PathVariable("businessId") Integer businessId) throws Exception {
		Business business = businessService.getBusinessById(businessId);
		//在商家微服务中调用食品微服务
		CommonResult<List> result = foodFeignClient.listFoodByBusinessId(businessId);
		if (result.getCode() == 200) {
			business.setFoodList(result.getResult());
		}else {
			business.setFoodList(new ArrayList());
		}
		return new CommonResult(200, "success(10301)", business);
	}

	 //positive sequence

	@RequestMapping("/listBusinessOrderByStarAsc")
	public CommonResult<List<Business>> listBusinessOrderByStarAsc() throws Exception{
		List<Business> list = businessService.listBusinessOrderByStarAsc();
		return new CommonResult(200, "success(10301)", list);
	}
	 
	 @RequestMapping("/listBusinessOrderBySalesAsc")
	 public CommonResult<List<Business>> listBusinessOrderBySalesAsc() throws Exception{
		List<Business> list =  businessService.listBusinessOrderBySalesAsc();
		return new CommonResult(200, "success(10301)", list);
	}
	 
	 //negative sequence
	 
	 @RequestMapping("/listBusinessOrderByStarDesc")
	 public CommonResult<List<Business>> listBusinessOrderByStarDesc() throws Exception{
		List<Business> list =  businessService.listBusinessOrderByStarDesc();
		return new CommonResult<>(200, "success(10301)", list);
	}
	 
	 @RequestMapping("/listBusinessOrderBySalesDesc")
	 public CommonResult<List<Business>> listBusinessOrderBySalesDesc() throws Exception{
		 List<Business> list =  businessService.listBusinessOrderBySalesDesc();
		 return new CommonResult<>(200, "success(10301)", list);
	}
	 
	  @RequestMapping("/addSalesByBusinessId")
	 public int addSalesByBusinessId(Business business) {
		return businessService.addSalesByBusinessId(business.getBusinessId());  }
	 
	 @RequestMapping("/searchBusiness") public List<Business>
	 searchBusiness(String keyWord){
		return businessService.searchBusiness(keyWord); }
}
